import axios from 'axios';
import { BASE_URL, TIMEOUT_MILLISECONDS } from '@/config';
import { getLocal, getQueryParams } from '@/utils/cache';
import { getVm } from '@/main';
var baseURL = BASE_URL;
let keyToken = getQueryParams('KeyToken');
if (keyToken) {
    document.cookie = 'mall_login_token=' + keyToken + ';path=/';
}
const instance = axios.create({
    baseURL,
    timeout: TIMEOUT_MILLISECONDS,
    headers: {
        'Content-Type': 'application/json',
        authorize: true,
    },
});

// if () {

// }

let loadInstance; // 页面加载效果
const hideLoading = function () {
    loadInstance && loadInstance.close();
    loadInstance = null;
};
console.log('baseURL', getLocal('token'));
instance.interceptors.request.use((config) => {
    // if (config.method === 'post') {
    //   config.data = JSON.stringify(config.data)
    // }
    // config.headers.Authorization = getLocal('token');
    if (getLocal('token')) {
        // axios.defaults.headers.common["token"] = getLocal("token");
        // token;
        // axios.defaults.headers["token"] = "eyJhbGciOiJIUzI1NiIsInR5cCIxNzEwMDc1Mzc1fQ.1i_6mAt-UsnWWkVqp3ot3lU5X3NjhU3ACCNcR2B9R30";
        // axios.defaults.headers["token"] = getLocal("token");
        config.headers.token = getLocal('token');
        // config.headers.Authorization = getLocal('token');
        // console.log('config.headers.Authorization', config.headers.Authorization);
    }
    // 显示页面加载效果
    if (config.loading === true && !loadInstance) {
        const vm = getVm();
        loadInstance = vm.$pageLoading.open();
    }

    let userInfo = JSON.parse(localStorage.getItem('userInfo'));
    if (userInfo && userInfo.loginToken) {
        config.headers.loginToken = userInfo.loginToken;
    } else if (keyToken) {
        config.headers.loginToken = keyToken;
    }

    return config;
});

instance.interceptors.response.use(
    (response) => {
        hideLoading();
        return response.data;
    },
    (err) => {
        if (err.response) {
            const { status, data } = err.response;
            if (status >= 500) {
                if (data.message) {
                    let msg = /[\u4e00-\u9fa5]+?.*[\u4e00-\u9fa5]+?/.exec(data.message);
                    let e = '';
                    if (msg && msg[0]) {
                        e = ',' + msg[0];
                    }
                    return Promise.reject('错误！服务器' + status + e);
                }
            }
            if (status == 404) {
                return Promise.reject('错误：404 ,请求的服务未找到');
            }
            if (status == 400) {
                return Promise.reject('错误：服务400');
            }
            if (status === 401) window.location.href = '/';
            // if (status === 403) eventBus.$router.push({ name: 'login' });
            return Promise.reject(err);
        } else {
            if (err.message === `timeout of ${TIMEOUT_MILLISECONDS}ms exceeded`) {
                Promise.reject('网络异常');
            }
            return Promise.reject(err);
        }
    },
);
export default instance;
